package demo5;

import java.util.Arrays;

class Student implements Comparable<Student>{
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public int compareTo(Student o) {
        if (this.age - o.age > 0) {
            return 1;
        }else if (this.age - o.age < 0) {
            return -1;
        }else {
            return 0;
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
    
}

public class Test {

    public static void main(String[] args) {
        Student[] array = new Student[3];
        array[0] = new Student("zhangsan",89,39.9);
        array[1] = new Student("lisi",69,59.9);
        array[2] = new Student("wangwu",39,68.4);
        System.out.println("排序前: " + Arrays.toString(array));
        // Arrays.sort(array);
        bubbleSort(array);
        System.out.println("排序后: " + Arrays.toString(array));
    }

    public static void bubbleSort(Comparable[] comparables) {
        for (int i = 0; i < comparables.length-1; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                if (comparables[j].compareTo(comparables[j + 1]) > 0) {
                    Comparable tmp = comparables[j + 1];
                    comparables[j] = comparables[j + 1];
                    comparables[j + 1] = tmp;
                }
            }
        }
    }
}